var arr
var xhr = new XMLHttpRequest()
xhr.responseType = "json"
xhr.open("GET", "http://192.168.1.105:8091/exceltoword/selectZrbhdshape?jcsj=2024Q3")
xhr.send()
xhr.onreadystatechange = function () {
  if (xhr.readyState == 4 && xhr.status == 200) {
    arr = xhr.response
    generate(arr)
    console.log(arr)
  }
}
function generate(arr) {
  var rows = [
      new docx.TableRow({
          children: [
              new docx.TableCell({
                  children: [new docx.Paragraph({ text: "序 号" }),],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [new docx.Paragraph({ text: "图斑编号" }),],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "保护地和湿地名称" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "变化情况" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "图斑类型" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "图斑类型编码" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "功能区" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "县" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "乡镇" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "经度" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "纬度" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
              new docx.TableCell({
                  children: [
                  new docx.Paragraph({ text: "面积（平面米）" }),
                  ],
                  verticalAlign: docx.VerticalAlign.CENTER,
              }),
          ],
      }),
  ]
  arr.forEach(item => {  
      rows.push(new docx.TableRow({  
          children: [  
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.fid}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),  
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.number}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),  
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.address}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.bhqk}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.tblx}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),  
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.tbbm}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),  
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.gnq}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.xianming}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.zhenming}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),  
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.lon}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),  
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.lat}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),
              new docx.TableCell({  
                  children: [new docx.Paragraph({ text: `${item.area}` })],  
                  verticalAlign: docx.VerticalAlign.CENTER,  
              }),
          ],  
      }));  
  });
  const doc = new docx.Document({
    sections: [
      {
        children: [
          new docx.Paragraph({
            text: "3.1成果分析",
            heading: docx.HeadingLevel.HEADING_1,
          }),
          new docx.Paragraph({
            text: "根据2024年三季度安徽省芜湖市高分辨率遥感影像，经过解译分析，芜湖市市域范围内自然保护地、重要湿地和名录湿地共发现疑似人类活动变化监测图斑143个，面积约303504.59平方米。按图斑类型分，矿产资源开发0个，工业开发3个，能源开发0个，旅游开发0个，交通开发14个，养殖开发7个，农业开发5个，居民点与其他活动81个，森林经营30个，工程建设3个；按变化类型分，新增图斑80个，面积为226461.94平方米；面积扩大图斑20个，面积为6283.74平方米；强度增强图斑29个，面积为42212.61平方米；面积减少图斑2个，面积为510.02平方米；强度减弱图斑12个，面积为28036.29平方米。",
          }),
          new docx.Paragraph({
            text: "芜湖市市域范围内省级以上自然保护地发现疑似人类活动变化监测图斑51个，面积为72197.21平方米。按图斑类型分，矿产资源开发0个，工业开发2个，能源开发0个，旅游开发0个，交通开发6个，养殖开发3个，农业开发1个，居民点与其他活动25个，森林经营14个，工程建设0个。按变化类型分，新增图斑30个，面积为56051.62平方米；面积扩大图斑11个，面积为4690.62平方米；强度增强图斑6个，面积为9959.47平方米；面积减少图斑1个，面积为242.92平方米；强度减弱图斑3个，面积为1252.59平方米。",
          }),
          new docx.Paragraph({
            text: "芜湖市市域范围内县级自然保护地发现疑似人类活动变化监测图斑89个，面积为230483.96平方米。按图斑类型分，矿产资源开发0个，工业开发1个，能源开发0个，旅游开发0个，交通开发8个，养殖开发4个，农业开发4个，居民点与其他活动54个，森林经营16个，工程建设2个。按变化类型分，新增图斑50个，面积为170410.33平方米；面积扩大图斑7个，面积为1166.80平方米；强度增强图斑22个，面积为31856.03平方米；面积减少图斑1个，面积为267.10平方米；强度减弱图斑9个，面积为26783.69平方米。",
          }),
          new docx.Paragraph({
            text: "芜湖市市域范围内重要湿地和名录湿地发现疑似人类活动变化监测图斑3个，面积为823.42平方米。按图斑类型分，矿产资源开发0个，工业开发0个，能源开发0个，旅游开发0个，交通开发0个，养殖开发0个，农业开发0个，居民点与其他活动2个，森林经营0个，工程建设1个。按变化类型分，面积扩大图斑2个，面积为426.32平方米；强度增强图斑1个，面积为397.10平方米。 ",
          }),
          new docx.Paragraph({
            text: "表3-1 芜湖市自然保护地遥感监测图斑统计表",
            heading: docx.HeadingLevel.HEADING_3,
            alignment: docx.AlignmentType.CENTER,
          }),
          new docx.Table({
            rows: rows
          }),
        ],
      },
    ],
  });

  var button1 = document.getElementById("button1")
  button1.addEventListener("click", function () {
      docx.Packer.toBlob(doc).then((blob) => {
      console.log(blob);
      saveAs(blob, "example.docx");
      console.log("Document created successfully");
    });
  })
}